package com.wc.算法提高课.B第二章_搜索.最小步数模型.魔板;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;

/**
 * @Author congge
 * @Date 2024/9/24 11:14
 * @description https://www.acwing.com/problem/content/1109/
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static char[][] g = new char[2][4];
    static Map<String, Integer> dist = new HashMap<>();
    static Map<String, String[]> pre = new HashMap<>();
    static Queue<String> q = new LinkedList<>();

    public static void main(String[] args) {
        String start = "", end = "";
        for (int i = 0; i < 8; i++) end += sc.next();
        for (int i = 1; i <= 8; i++) start += (char) (i + '0');
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 3; j++) {
                g[i][j] = ' ';
            }
        }
        bfs(start, end);
        out.println(dist.get(end));
        StringBuffer res = new StringBuffer();
        while (!end.equals(start)) {
            String[] ss = pre.get(end);
            res.insert(0, ss[1]);
            end = ss[0];
        }
        if (res.length() != 0)
            out.println(res);
        out.flush();
    }

    static void bfs(String start, String end) {
        q.add(start);
        dist.put(start, 0);
        if (start.equals(end)) return;
        while (!q.isEmpty()) {
            String t = q.poll();
            int d = dist.get(t);
            String[] m = new String[3];
            m[0] = move0(t);
            m[1] = move1(t);
            m[2] = move2(t);
            for (int i = 0; i < 3; i++) {
                if (!dist.containsKey(m[i])) {
                    dist.put(m[i], d + 1);
                    pre.put(m[i], new String[]{t, String.valueOf((char) (i + 'A'))});
                    if (m[i].equals(end)) return;
                    q.add(m[i]);
                }
            }
        }
    }

    static void set(String state) {
        char[] s = state.toCharArray();
        for (int i = 0; i < 4; i++) g[0][i] = s[i];
        for (int i = 3, j = 4; i >= 0; i--, j++) g[1][i] = s[j];
    }

    static String get() {
        StringBuilder res = new StringBuilder();
        for (int i = 0; i < 4; i++) res.append(g[0][i]);
        for (int i = 3; i >= 0; i--) res.append(g[1][i]);
        return res.toString();
    }

    static String move0(String state) {
        set(state);
        char[] tmp = g[0];
        g[0] = g[1];
        g[1] = tmp;
        return get();
    }

    static String move1(String state) {
        set(state);
        char v0 = g[0][3], v1 = g[1][3];
        for (int i = 2; i >= 0; i--) {
            for (int j = 0; j <= 1; j++) {
                g[j][i + 1] = g[j][i];
            }
        }
        g[0][0] = v0;
        g[1][0] = v1;
        return get();
    }

    static String move2(String state) {
        set(state);
        char tmp = g[0][1];
        g[0][1] = g[1][1];
        g[1][1] = g[1][2];
        g[1][2] = g[0][2];
        g[0][2] = tmp;
        return get();
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
